package com.ghs.jms;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

public class MessageReceiver {
	
	public static void Receiver(String[] args) {
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		try {
			connection = ConnUtils.createConnection(SystemSettings.BROKER_URL);
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue(SystemSettings.QUEUE);
			consumer = session.createConsumer(destination);
			receiverMessage(consumer, args);
		} catch (JMSException e) {
			e.printStackTrace();
		}finally{
			ConnUtils.free(session, connection);
		}
	}
	

	public static void receiverMessage(MessageConsumer consumer,String[] args){
		while (true) {
			TextMessage message;
			try {
				message = (TextMessage) consumer.receive(500000);
				if (message != null) {
					System.out.println("收到：" + message.getText());
					SendEmail.main(args);
				} else {
					break;
				}
			} catch (JMSException e) {
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
